package com.ewzj.bigdata.mvp.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.bumptech.glide.Glide
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.di.component.DaggerEditProfileActivityComponent
import com.ewzj.bigdata.di.module.EditProfileActivityModule
import com.ewzj.bigdata.mvp.contract.EditProfileActivityContract
import com.ewzj.bigdata.mvp.model.bean.UserBean
import com.ewzj.bigdata.mvp.presenter.EditProfileActivityPresenter
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import kotlinx.android.synthetic.main.activity_edit_profile.*
import me.iwf.photopicker.PhotoPicker


class EditProfileActivity : BaseActivity<EditProfileActivityPresenter>(),EditProfileActivityContract.View {
    val userinfo:UserBean by lazy { intent.getSerializableExtra(AppKey.EXTRA_DATA) as UserBean}
    lateinit var  mSelected:List<Uri>
    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerEditProfileActivityComponent.builder().appComponent(appComponent)
                .editProfileActivityModule(EditProfileActivityModule(this))
                .build().inject(this)
    }

    override fun initData(savedInstanceState: Bundle?) {
        tv_title.text="编辑资料"
        initlistener()
        updateView(userinfo)
    }

    private fun initlistener() {
        RxView.clicks(iv_back).subscribe({finish()})
        RxView.clicks(layout_name).subscribe({EditActivity.startEditActivity(this, tv_name.getText().toString(), false, "昵称")})
        RxView.clicks(layout_sign).subscribe({EditActivity.startEditActivity(this, tv_sign.getText().toString(), true, "个性签名")})
        RxView.clicks(layout_sex).subscribe({
            mPresenter.updateSex(userinfo)
        })
        RxView.clicks(ll_head).subscribe({
            PhotoPicker.builder()
                    .setPhotoCount(1)
                    .setShowCamera(true)
                    .setShowGif(false)
                    .setPreviewEnabled(false)
                    .start(this, PhotoPicker.REQUEST_CODE)
        })
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_edit_profile
    }

    override fun showLoading() {
    }

    override fun launchActivity(intent: Intent?) {
    }

    override fun hideLoading() {
    }

    override fun killMyself() {
    }

    override fun showMessage(message: String?) {
    }

    override fun useFragment(): Boolean {
        return false
    }

    override fun updateView(user: UserBean) {
        Glide.with(this).load(Apputils.Utils.getImgUrl(user.avatar?:"error")).asBitmap().dontTransform().dontAnimate().error(R.drawable.ic_loading)
                .placeholder(R.drawable.ic_loading).into(iv_avatar)
        tv_name.text=user.nickname?:"觅窝新用户"
        tv_sex.text=if (user.getSex().equals("1")) "男" else "女"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!= Activity.RESULT_CANCELED&&data!=null){
            when(requestCode){
                AppKey.REQUEST_SIGNCODE->{
                    var change=data.extras.get(AppKey.EXTRA_DATA)
                    userinfo.sign=change as String
                    updateView(userinfo)
                    mPresenter.updateUserInfo(userinfo)
                }
                AppKey.REQUEST_CODE->{
                    var change=data.extras.get(AppKey.EXTRA_DATA)
                    userinfo.nickname=change as String
                    updateView(userinfo)
                    mPresenter.updateUserInfo(userinfo)
                }
                PhotoPicker.REQUEST_CODE->{
                    if (data != null) {
                        val photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS)
                        if (photos!=null&&photos.size>0){
                            Glide.with(this).load(photos[0]).asBitmap().dontTransform().dontAnimate().error(R.drawable.ic_loading)
                                    .placeholder(R.drawable.ic_loading).into(iv_avatar)
//                            iv_avatar.loadImgwithloading(photos[0])
                            mPresenter.uploadPic(photos[0])
                        }

                    }
                }
            }
        }
    }

    override fun updateUserhead(userhead: String) {
        userinfo.avatar=Apputils.Utils.getImgUrl(userhead)

        Glide.with(this).load(userinfo.avatar).asBitmap().dontTransform().dontAnimate().error(R.drawable.ic_loading)
                .placeholder(R.drawable.ic_loading).into(iv_avatar)
//        iv_avatar.loadImgwithloading(userinfo.avatar)
        mPresenter.updateUserInfo(userinfo)
    }

    companion object{
        fun startEditProfileActivity(context: Context, userbean: UserBean) {
            val intent = Intent(context, EditProfileActivity::class.java)
            intent.putExtra(AppKey.EXTRA_DATA, userbean)
            context.startActivity(intent)
        }
    }

    override fun changeSex(sex: String) {
        tv_sex.text = sex
    }
}
